package com.mercycrazy.recruit.entity;

import com.mercycrazy.recruit.user.AdminInfo;
import com.mercycrazy.recruit.user.SimpleAdminInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * Admin 实体类定义
 *
 * @author <a href="mailto:1443424326@qq.com">mercycrazy</a>
 * @since 2023-01-10
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "t_admin")
public class Admin implements Serializable {

    /** 自增主键 */
    @Id
    @GeneratedValue(generator = "SnowIdGenerator")
    @GenericGenerator(name = "SnowIdGenerator", strategy = "com.mercycrazy.recruit.support.jpa.SnowIdGenerator")
    @Column(name = "id", nullable = false)
    private String id;

    /** 登录名 */
    @Column(name = "username", nullable = false)
    private String username;

    /** 密码 */
    @Column(name = "password", nullable = false)
    private String password;

    /** 盐 - 用来对密码混合用户名加盐加密 */
    @Column(name = "salt", nullable = false)
    private String salt;

    /** 备注 */
    @Column(name = "remark", nullable = false)
    private String remark;

    /** 头像 */
    @Column(name = "face", nullable = false)
    private String face;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 将 AdminInfo 转成实体对象
     */
    public static Admin to(AdminInfo adminInfo) {

        Admin admin = new Admin();

        admin.setId(adminInfo.getId());
        admin.setUsername(adminInfo.getUsername());
        admin.setPassword(adminInfo.getPassword());
        admin.setSalt(adminInfo.getSalt());
        admin.setRemark(adminInfo.getRemark());
        admin.setFace(adminInfo.getFace());
        admin.setCreateTime(adminInfo.getCreateTime());
        admin.setUpdateTime(adminInfo.getUpdateTime());

        return admin;
    }

    /**
     * 将实体对象转成 AdminInfo 对象
     */
    public AdminInfo toAdminInfo() {

        AdminInfo adminInfo = new AdminInfo();

        adminInfo.setId(this.id);
        adminInfo.setUsername(this.username);
        adminInfo.setPassword(this.password);
        adminInfo.setSalt(this.salt);
        adminInfo.setRemark(this.remark);
        adminInfo.setFace(this.face);
        adminInfo.setCreateTime(this.createTime);
        adminInfo.setUpdateTime(this.updateTime);

        return adminInfo;
    }

    /**
     * 将实体对象转成 SimpleAdminInfo 对象
     */
    public SimpleAdminInfo toSimple() {

        SimpleAdminInfo simpleAdminInfo = new SimpleAdminInfo();

        simpleAdminInfo.setId(this.id);
        simpleAdminInfo.setUsername(this.username);
        simpleAdminInfo.setRemark(this.remark);
        simpleAdminInfo.setFace(this.face);

        return simpleAdminInfo;
    }
}